package com.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Ticket {
    public Integer sum = 30;

    public final ReentrantLock reentrantLock = new ReentrantLock(true);

    public Integer saleTicket() {
        try {
            reentrantLock.lock();
            if (sum > 0) {
                //可重入锁，锁两次，就要解两次 否则死锁
//                reentrantLock.lock();
//                Condition condition = reentrantLock.newCondition();
//                condition.wait();
                int k = sum--;
                return k;
            } else {
                return null;
            }

        }finally {
            reentrantLock.unlock();
        }

    }
}
